<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Basic usage</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pgsql.examples-basic.html">« Basic usage</a></li>
      <li style="float: right;"><a href="ref.pgsql.html">PostgreSQL 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="pgsql.examples.html">范例</a></li>
    <li>Basic usage</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pgsql.examples-queries" class="section">
  <h2 class="title">Basic usage</h2>
  <p class="para">
   These examples contain user defined functions similar to legacy MySQL
   functions.

   <div class="example" id="example-1886">
    <p><strong>示例 #1 PostgreSQL user defined functions example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;function&nbsp;should&nbsp;be&nbsp;needed,&nbsp;since&nbsp;PostgreSQL&nbsp;connection&nbsp;binds&nbsp;database.<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">pg_list_dbs</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">is_resource</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'<br />SELECT<br />&nbsp;d.datname&nbsp;as&nbsp;"Name",<br />&nbsp;u.usename&nbsp;as&nbsp;"Owner",<br />&nbsp;pg_encoding_to_char(d.encoding)&nbsp;as&nbsp;"Encoding"<br />FROM<br />&nbsp;pg_database&nbsp;d&nbsp;LEFT&nbsp;JOIN&nbsp;pg_user&nbsp;u&nbsp;ON&nbsp;d.datdba&nbsp;=&nbsp;u.usesysid<br />ORDER&nbsp;BY&nbsp;1;<br />'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;List&nbsp;tables.<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">pg_list_tables</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">is_resource</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"<br />SELECT<br />&nbsp;c.relname&nbsp;as&nbsp;\"Name\",<br />&nbsp;CASE&nbsp;c.relkind&nbsp;WHEN&nbsp;'r'&nbsp;THEN&nbsp;'table'&nbsp;WHEN&nbsp;'v'&nbsp;THEN&nbsp;'view'&nbsp;WHEN&nbsp;'i'&nbsp;THEN&nbsp;'index'&nbsp;WHEN&nbsp;'S'&nbsp;THEN&nbsp;'sequence'&nbsp;WHEN&nbsp;'s'&nbsp;THEN&nbsp;'special'&nbsp;END&nbsp;as&nbsp;\"Type\",<br />&nbsp;&nbsp;u.usename&nbsp;as&nbsp;\"Owner\"<br />FROM<br />&nbsp;pg_class&nbsp;c&nbsp;LEFT&nbsp;JOIN&nbsp;pg_user&nbsp;u&nbsp;ON&nbsp;c.relowner&nbsp;=&nbsp;u.usesysid<br />WHERE<br />&nbsp;c.relkind&nbsp;IN&nbsp;('r','v','S','')<br />&nbsp;AND&nbsp;c.relname&nbsp;!~&nbsp;'^pg_'<br />ORDER&nbsp;BY&nbsp;1;<br />"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;See&nbsp;also&nbsp;pg_meta_data().&nbsp;It&nbsp;returns&nbsp;field&nbsp;definition&nbsp;as&nbsp;array.<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">pg_list_fields</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$table</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">is_resource</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"<br />SELECT<br />&nbsp;a.attname,<br />&nbsp;format_type(a.atttypid,&nbsp;a.atttypmod),<br />&nbsp;a.attnotnull,<br />&nbsp;a.atthasdef,<br />&nbsp;a.attnum<br />FROM<br />&nbsp;pg_class&nbsp;c,<br />&nbsp;pg_attribute&nbsp;a<br />WHERE<br />&nbsp;c.relname&nbsp;=&nbsp;'"</span><span style="color: #007700">.</span><span style="color: #0000BB">$table</span><span style="color: #007700">.</span><span style="color: #DD0000">"'<br />&nbsp;AND&nbsp;a.attnum&nbsp;&gt;&nbsp;0&nbsp;AND&nbsp;a.attrelid&nbsp;=&nbsp;c.oid<br />ORDER&nbsp;BY&nbsp;a.attnum;<br />"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div></div></div></body></html>